﻿using MediatR;
using W.Purchase.Domain.DTO;
using W.Purchase.Read.API.AppLication.Command.ProcurementCommands;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.AppLication.CommandHandler.ProcurementCommandsHandler
{
    public class ProcurementFanCommandHandler : IRequestHandler<ProcurementFanCommand, ApiResult<ProcurementFanDTO>>
    {
        private readonly ILogger<ProcurementFanCommandHandler> _logger;
        private readonly IProcurementRepository _procurementRepository;
        private readonly IBuyProductsRepository _buyProductsRepository;
        private readonly IVendorRepository _vendorRepository;
        public ProcurementFanCommandHandler(ILogger<ProcurementFanCommandHandler> logger, IProcurementRepository procurementRepository, IBuyProductsRepository buyProductsRepository, IVendorRepository vendorRepository)
        {
            _logger = logger;
            _procurementRepository = procurementRepository;
            _buyProductsRepository = buyProductsRepository;
            _vendorRepository = vendorRepository;
        }
        public async Task<ApiResult<ProcurementFanDTO>> Handle(ProcurementFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ProcurementFanDTO> result = new ApiResult<ProcurementFanDTO>();
            try
            {
                _logger.LogInformation("反填采购成功");
                var products = await _buyProductsRepository.GetAllAsaync();
                var procurement = from a in await _procurementRepository.GetAllAsaync()
                                  join b in await _vendorRepository.GetAllAsaync() on a.VendorNo equals b.VendorNo
                              where a.IsDelete == false && a.ProcurementId == request.procurementId
                              select new ProcurementFanDTO
                              {
                                  ProcurementNo = a.ProcurementNo,
                                  ProcurementId = a.ProcurementId,
                                  ProcurementTypeName = a.ProcurementTypeName,
                                  AssociateNumber = a.AssociateNumber,
                                  VendorNo = a.VendorNo,
                                  VendorName = b.VendorName,
                                  ClientName = a.ClientName,
                                  ProcurementPerson = a.ProcurementPerson,
                                  ProcurementPhone = a.ProcurementPhone,
                                  Harvestay = a.Harvestay,
                                  ProcurementRemark = a.ProcurementRemark,
                                  SingleMaker = a.SingleMaker,
                                  SingleTime = a.SingleTime,
                                  //ProcurementState = a.ProcurementState,
                                  //ProcurementStateText = a.ProcurementState.ToString(),
                                  //IsRecorded = a.IsRecorded,
                                  //IsReturn = a.IsReturn,
                                  CreateTime = a.CreateTime,
                                  BuyProductsData = products.Where(x => x.ProcurementId == a.ProcurementId).Select(x => new BuyProductsData
                                  {
                                      BuyProductsId = x.BuyProductsId,
                                      ProcurementId = x.ProcurementId,
                                      ProductNo = x.ProductNo,
                                      ProductName = x.ProductName,
                                      ProcurementState = x.ProcurementState,
                                      ProcurementStateText = x.ProcurementState.ToString(),
                                      IsRecorded = x.IsRecorded,
                                      IsReturn = x.IsReturn,
                                      ReturnNo = x.ReturnNo,
                                      ReturnsNum = x.ReturnsNum,
                                      Batch = x.Batch,
                                      ProductSpec = x.ProductSpec,
                                      UnitName = x.UnitName,
                                      Price = x.Price,
                                      BuyProductsNum = x.BuyProductsNum,
                                      BuyProductsTotal = x.BuyProductsTotal,
                                  }).ToList()
                              };
                result.Data = procurement.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
            }
            return result;
        }
    }
}
